import 'dart:async';

import 'package:flutter/material.dart';
import 'package:im_flutter_sdk_example/pages/toolbar/alliance/widgets/timeline_list.dart';
import 'package:im_flutter_sdk_example/provider/moment_model.dart';
import 'package:im_flutter_sdk_example/utils/event_bus.dart';
import 'package:provider/provider.dart';

/*
 *@功能: 小组动态
 *@author: lovecrosszhu
 *@date: 2021-02-02 16:44:02
 *@version: V1.0.0
*/
class TimeLineList extends StatefulWidget {
  @override
  _TimeLineState createState() => _TimeLineState();
}

class _TimeLineState extends State<TimeLineList>
    with AutomaticKeepAliveClientMixin {
  List tab = [
    {'title': '最新', 'key': 0},
    {'title': '最热', 'key': 1},
  ];

  int active = 0;
  StreamSubscription _momentSubscription;

  change(int index) {
    setState(() {
      active = index;
    });
  }

  @override
  void initState() {
    super.initState();
    this._reqMoments();
    _momentSubscription = eventBus.on<MomentEvent>().listen((event) {
      this._reqMoments();
    });
  }

  dispose() {
    super.dispose();
    _momentSubscription.cancel();
  }

  @override
  Widget build(BuildContext context) {
    super.build(context);
    return Container(
      child: TimelineList(),
    );
  }

  void _reqMoments() async {
    Future.delayed(Duration(milliseconds: 2), () {
      MomentProvider _moment =
          Provider.of<MomentProvider>(context, listen: true);
      _moment.loadMoment();
    });
  }

  @override
  bool get wantKeepAlive => true;
}
